import 'dart:math';

import 'package:base_flutter_project/pages/speak_page.dart';
import 'package:base_flutter_project/plugin/asr_manager.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class TravelPage extends StatefulWidget {
  @override
  _TravelPageState createState() => _TravelPageState();
}

class _TravelPageState extends State<TravelPage> {
  bool isclear = false;
  TextEditingController _editingController;
  final List<String> _dataSource = List<String>.generate(100, (idx) => Random().nextInt(10000).toString());
  final List<String> _searchSource = [];

  @override
  initState() {
    super.initState();
    _editingController = TextEditingController();
  }

  Widget _normalSearch() {
    return Container(
      height: 60,
      margin: const EdgeInsets.only(top: 20),
      padding: const EdgeInsets.symmetric(horizontal: 16),
      color: Colors.grey[300],
      child: Row(
        children: <Widget>[
          Icon(
            Icons.search,
            size: 20,
          ),
          Expanded(
            child: TextField(
              onChanged: _textOnChnage,
              controller: _editingController,
              cursorColor: Colors.black,
              style: TextStyle(
                fontSize: 18,
                color: Colors.black,
              ),
              decoration: InputDecoration(
                  contentPadding: EdgeInsets.fromLTRB(5, 0, 5, 0),
                  border: InputBorder.none,
                  hintText: "请输入...",
                  hintStyle: TextStyle(fontSize: 15, color: Colors.grey[400])),
            ),
          ),
          GestureDetector(
            child: isclear ? Icon(Icons.close) : Icon(Icons.mic),
            onTap: () {
              if (isclear) {
                setState(() {
                  isclear = false;
                  _searchSource.removeRange(0, _searchSource.length);
                });
                _editingController.clear();
              } else {
                Navigator.of(context).push(MaterialPageRoute(builder: (context) => SpeakPage()));
              }
            },
          )
        ],
      ),
    );
  }

  _textOnChnage(String text) {
    if (text == "") {
      setState(() {
        isclear = false;
        _searchSource.removeRange(0, _searchSource.length);
      });
    } else {
      setState(() {
        isclear = true;
        _searchSource.removeRange(0, _searchSource.length);
        _dataSource.forEach((t){
          if(t.indexOf(_editingController.text) >= 0){
            _searchSource.add(t);
          }
        });
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Column(
      children: <Widget>[
        _normalSearch(),
        Expanded(
          child: ListView.builder(
            itemBuilder: (context, idx){
              String str;
              Widget wid;
              if(_searchSource.length > 0){
                str = "data____${_searchSource[idx]}";
                List<String> array = str.split(_editingController.text);
                List<String> list = [];
                list.add(array[0]);
                list.add(_editingController.text);
                list.add(array[1]);
                final children = list.map((val){
                  return TextSpan(text: val,style: TextStyle(color: val == _editingController.text ? Colors.red : Colors.black));
                });
                wid = RichText(
                  text: TextSpan(
                    children: children.toList()
                  ),
                );
              } else {
                str = "data____${_dataSource[idx]}";
                wid = Text(str);
              }
              return Container(
                height: 55,
                padding: const EdgeInsets.only(left: 16),
                child: wid
              );
            },
            itemCount: _editingController.text.length > 0 ? _searchSource.length : _dataSource.length,
          ),
        )
      ],
    ));
  }
}
